/**
 * Created by xieky on 2017/6/22.
 */
define(function (require, exports, module) {
    var angular = require('angular');
    require("pagination");
    require("angular-ui");
    require('/public/js/myservice/common/nav');
    var authorize = require('/public/js/myservice/authorize');
    var config = require('/public/js/myservice/config');
    var util = require("util");
    var api_prefix = G_CONFIG["api_prefix"]; //api前缀
    require('/public/js/myservice/common/row-toolbar');

    angular.module('backlogHaveDone',['ngUI','myNav','rowToolbar'])
        .service('backlogService',['$rootScope', function ($rootScope) {
            var service={
                onBacklogShow: function ($dialog, $dialogScope, deliveryguid,ordercode) {

                    $dialogScope.deliveryguid = deliveryguid;

                    //初始化信息
                    $dialogScope.remark = "";
                    $dialogScope.ylTents = [];
                    $dialogScope.ylFiles = [];
                    $dialogScope.ekpcontractcode = "";
                    $dialogScope.ekpcontractguid = "";
                    $dialogScope.order_code = ordercode;

                    //查询执行
                    $dialogScope.getDataDetail={
                        doExecute:function(deliveryguid){
                            var parms = {deliveryGuid:deliveryguid};
                            var url = api_prefix + "/out-storage/get-delivery-detail";
                            util.myAjax({
                                url:url,
                                type:"POST",
                                context:this,
                                data:parms,
                                success:function(resultdata){
                                    if(resultdata.retCode == '0') {
                                        $dialogScope.remark = resultdata.data.YlInfo.Remark;
                                        $dialogScope.ylTents = resultdata.data.YlInfo.Yl_Tenant;
                                        $dialogScope.ylFiles = resultdata.data.YlInfo.Files;
                                        if(resultdata.data.YlInfo.Yl_Tenant[0] && resultdata.data.YlInfo.Yl_Tenant[0].Erp_Products[0]){
                                            $dialogScope.ekpcontractcode = resultdata.data.YlInfo.Yl_Tenant[0].Erp_Products[0].Signno;
                                            $dialogScope.ekpcontractguid = resultdata.data.YlInfo.Yl_Tenant[0].Erp_Products[0].Signguid;
                                        }
                                        $dialogScope.$applyAsync();
                                    }else{
                                        util.alert(resultdata.errMsg);
                                    }
                                }
                            });
                        }
                    };

                    $dialogScope.getDataDetail.doExecute(deliveryguid);
                },
                onWorkflowShow: function ($dialog, $dialogScope, deliveryguid) {

                    $dialogScope.deliveryguid = deliveryguid;
                    $dialogScope.workflowlist = [];

                    //查询执行
                    $dialogScope.getWorkListDetail={
                        doExecute:function(deliveryguid){
                            var parms = {deliveryGuid:deliveryguid};
                            var url = api_prefix + "/out-storage/get-workflow-process-list";
                            util.myAjax({
                                url:url,
                                type:"POST",
                                context:this,
                                data:parms,
                                success:function(resultdata){
                                    if(resultdata.retCode == '0') {
                                        $dialogScope.workflowlist = resultdata.data;
                                        $dialogScope.$applyAsync();
                                    }else{
                                        util.alert(resultdata.errMsg);
                                    }
                                }
                            });
                        }
                    };

                    $dialogScope.getWorkListDetail.doExecute(deliveryguid);
                },
                editBacklogSave:function($data, $dialog){
                    //util.alert($data.name);
                }
            };

            return service;
        }])
        .controller('backlogListHaveDoneCtrl',['$scope','backlogService','appservice',function($scope,backlogService,appservice){
            appservice.name = '明源云链-已办';
            $scope.backlogService = backlogService;

            $scope.filterDataType = [
                {name: '全部', selected: true, value: ''},
                {name: '正式产品', value: '正式产品'},
                {name: '特批产品', value: '特批产品'}
            ];

            $scope.filterDataStatus= [
                {name: '全部', selected: true, value: '21,23,24,25'},
                // {name: '审批', value: '20'},
                {name: '确认', value: '21'},
                // {name: '开通', value: '22'},
                {name: '授权', value: '23'},
                // {name: '打回',value: '10'},
                {name: '已上传接收单', value: '24'},
                {name: '已归档', value: '25'}
            ];

            //查询执行
            $scope.getData={
                keyword: '',
                doExecute:function(pageindex){
                    var parms = $scope.getQueryParams(pageindex);
                    var url = api_prefix + "/out-storage/get-delivery-list";
                    util.myAjax({
                        url:url,
                        type:"POST",
                        context:this,
                        data:parms,
                        success:function(resultdata){
                            if(resultdata.retCode == '0') {
                                $scope.deliverylist = resultdata.data.rows;
                                $scope.$applyAsync();

                                $scope.startPageIndex = (parms.pageindex - 1) * parms.pagesize;
                                $('#pagination').pagination(parseInt(resultdata.data.total), {
                                    current_page: parms.pageindex - 1,
                                    num_edge_entries: 1, //边缘页数
                                    num_display_entries: 4, //主体页数

                                    callback: function (page_index, jq) {
                                        $scope.startPageIndex = (page_index - 1) * parms.pagesize;
                                        $scope.getData.doExecute(page_index);
                                    },
                                    items_per_page: 20 //每页显示1项
                                });
                            }else{
                                util.alert(resultdata.errMsg);
                            }
                        }
                    });
                }
            };

            $scope.getQueryParams= function (pageindex) {
                var param = {keyword: $scope.getData.keyword};
                param.pageindex = pageindex == null?1:pageindex;
                param.pagesize = 20;

                filterDataType = $.grep($scope.filterDataType, function (obj,idx) {
                    return obj.selected == 1;
                })
                if(filterDataType.length){
                    param.datatype = filterDataType[0].value;
                }

                filterStatus = $.grep($scope.filterDataStatus, function (obj,idx) {
                    return obj.selected == 1;
                })
                if(filterStatus.length){
                    param.status = filterStatus[0].value;
                }

                return param;
            };

            $scope.startProcessApprove = function(busiGUID){
                var parms = {deliveryGuid: busiGUID};
                var url = api_prefix + "/out-storage/workflow-url";
                util.myAjax({
                    url:url,
                    type:"POST",
                    context:this,
                    data:parms,
                    success:function(resultdata){
                        if(resultdata.retCode == '0'){
                            window.open('http://' + resultdata.data);
                        }else{
                            util.alert(resultdata.errMsg);
                        }
                    }
                });
            };

            $scope.addenant = function(busiGUID){
                var parms = {deliveryGuid: busiGUID};
                var url = api_prefix + "/out-storage/add-tenant";
                util.myAjax({
                    url:url,
                    type:"POST",
                    context:this,
                    data:parms,
                    success:function(resultdata){
                        if(resultdata.retCode == '0'){
                            util.alert(resultdata.errMsg);
                            $scope.getData.doExecute();
                        }else{
                            util.alert(resultdata.errMsg);
                        }
                    }
                });
            };
            
            $scope.accountAuth = function (busiGUID) {
                var parms = {deliveryGuid: busiGUID};
                var url = api_prefix + "/out-storage/auth-tenant";
                util.myAjax({
                    url:url,
                    type:"POST",
                    context:this,
                    data:parms,
                    success:function(resultdata){
                        if(resultdata.retCode == '0'){
                            util.alert(resultdata.errMsg);
                            $scope.getData.doExecute();
                        }else{
                            util.alert(resultdata.errMsg);
                        }
                    }
                });
            };

            $scope.refuseApprove = function (busiGUID) {
                var strReason = prompt("请录入打回原因:","");
                if(!strReason){
                    strReason = "";
                }

                var parms = {deliveryGuid: busiGUID};
                parms.status = 10;
                parms.reason = strReason;
                var url = api_prefix + "/out-storage/update-delivery-state";
                util.myAjax({
                    url:url,
                    type:"POST",
                    context:this,
                    data:parms,
                    success:function(resultdata){
                        if(resultdata.retCode == '0'){
                            util.alert("打回成功");
                            $scope.getData.doExecute();
                        }else{
                            util.alert(resultdata.errMsg);
                        }
                    }
                });
            };

            $scope.recoverApprove = function (busiGUID) {
                var parms = {deliveryGuid: busiGUID};
                parms.status = 20;
                var url = api_prefix + "/out-storage/update-delivery-state";
                util.myAjax({
                    url:url,
                    type:"POST",
                    context:this,
                    data:parms,
                    success:function(resultdata){
                        if(resultdata.retCode == '0'){
                            util.alert("还原成功");
                            $scope.getData.doExecute();
                        }else{
                            util.alert(resultdata.errMsg);
                        }
                    }
                });
            };

            $scope.changeType = function () {
                $scope.getData.doExecute();
            };

            $scope.changeStatus = function () {
                $scope.getData.doExecute();
            };

            $scope.look = function (url) {
                alert(url)
                console.log(url)
            };
            $scope.down = function (url) {
                alert(url)
                console.log(url)
            };

            $scope.getData.doExecute();

        }])
})